<?php

namespace App;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class AppToken extends Controller
{
    //
    private $lifetime;

    public function __construct()
    {
        $this->lifetime=3600 * 24 * 30;        //过期时间
    }

    /**
     * 创建Session
     * @param $uid 用户id
     * @param $value value值
     * @return string 返回Session_id
     */
    public function Create($uid,$value,$vcode=null)
    {
        $data=AppSession::where("user_id",$uid)->where("value",$value)->first();
        if($data!=null){
            $create["life_time"]=time()+$this->lifetime;
            $create["id"]=md5(uniqid("$uid",1));
            $create["value"]=$value;
            $create["vcode"]=$vcode;
            $data->update($create);
            $result=$create["id"]."_"."$uid";
        }
        else {
            $create["life_time"]=time()+$this->lifetime;
            $create["id"]=md5(uniqid("$uid",1));
            $create["user_id"]=$uid;
            $create["value"]=$value;
            $create["vcode"]=$vcode;
            AppSession::create($create);
            $result=$create["id"]."_"."$uid";
        }
        return $result;
    }


    /**
     * 删除用户 某个value的session
     * @param $uid  用户id
     * @param $value  用户value值
     * @return bool   成功返回true  失败返回false
     */
    public function Delete($uid,$value)
    {
        $data=AppSession::where("user_id",$uid)->where("value",$value)->first();
        if($data!=null){
            $data->delete();
            return true;
        }
        else return false;
    }

    /**
     * 删除该用户的所有session值
     * @param $uid  用户id
     * @return bool  成功返回true  失败返回false
     */
    public function Delete_All($uid)
    {
        $data=AppSession::where("user_id",$uid)->get();
        if($data!=null){
            foreach($data as $arr){
                $arr->delete();
            }
            return true;
        }
        else return false;
    }

    /**
     * 验证Session_id
     * @param $Session_id
     * @return bool|string  成功则返回用户id 失败返回false
     */
    public function ValidateSession($Session_id)
    {
        $uid=substr(strrchr($Session_id,'_'),1);
        $id=substr($Session_id,0,strrpos($Session_id,"_"));
        $data=AppSession::where("id",$id)->first();
        if($data!=null){
            if($data->user_id!=$uid) return false;                                               //session_id 与用户id不匹配
            else {
                if(time()-($data->life_time)>$this->lifetime) {$data->delete(); return false;}  //Session_id过期 返回假并且删除旧session
                else return $uid;                                                               //验证成功
            }
        }
        else return false;                                                                      //session_id不存在
    }


    /**
     * 通过uid 以及 value 来验证session
     * @param $uid  用户名
     * @param $value  value值
     * @param $vcode   验证码(默认为null)
     * @return bool|string 成功返回session_id 失败返回false
     */
    public function ValidateValue($uid,$value,$vcode=null){
        $data=AppSession::where("user_id",$uid)->where("value",$value)->first();
        if($data!=null){
            if(time()-($data->life_time)>$this->lifetime) {$data->delete(); return false;}      //session已经过期
            else {
                if(!$vcode) return $data->id;                                                   //验证成功(无验证码)
                else if($data->vcode==$vcode) return $data->id;                                //验证成功(有验证码)
                else return false;                                                             //验证失败(验证码错误)
            }
        }
        else return false;           															//数据库找不到数据
    }
}
